//
//  KYLoginController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/28.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYLoginController.h"
#import "KYLoginView.h"

@interface KYLoginController ()<KYLoginViewDelegate>

@end

@implementation KYLoginController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.创建loginView
    [self setUpLoginView];
    
    // 2.设置关闭按钮
    [self setNav];
    
    // 2.创建导航栏上的按钮
    [self setUpNavBarItem];
    
}

- (void)setUpNavBarItem
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:NSLocalizedString(@"返回", nil) titleColor:[UIColor blackColor] target:self action:@selector(back)];
}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setUpLoginView
{
    KYLoginView *loginView = [KYLoginView customLoginView];
    loginView.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64);
    loginView.delegate = self;
    [self.view addSubview:loginView];
}
- (void)setNav{
#warning - 后续换成图标
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    [right setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = right;
}
- (void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - KYLoginViewDelegate
- (void)loginView:(KYLoginView *)loginView didClickPostCodeButton:(KYCodeButton *)button
{
    // 0.按钮不能点击
    button.enabled = NO;
    [button setTitle:NSLocalizedString(@"已发送", nil) forState:UIControlStateNormal];
    
    // 1.创建一个按钮
    UIButton *btn = [[UIButton alloc] init];
    // below : 下面  btn会显示在self.navigationController.navigationBar的下面
    [self.navigationController.view insertSubview:btn belowSubview:self.navigationController.navigationBar];
    
    // 2.设置图片和文字
    btn.userInteractionEnabled = NO;
    [btn setBackgroundImage:[UIImage resizedImageWithName:@"timeline_new_status_background"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitle:NSLocalizedString(@"短信已发送,请注意查收", nil) forState:UIControlStateNormal];
    
    // 3.设置按钮的初始frame
    CGFloat btnH = 35;
    CGFloat btnY = 64 - btnH;
    CGFloat btnX = 2;
    CGFloat btnW = self.view.frame.size.width - 2 * btnX;
    btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    
    // 4.通过动画移动按钮(按钮向下移动 btnH + 1)
    [UIView animateWithDuration:0.7 animations:^{
        
        btn.transform = CGAffineTransformMakeTranslation(0, btnH + 2);
        
    } completion:^(BOOL finished) { // 向下移动的动画执行完毕后
        
        // 建议:尽量使用animateWithDuration, 不要使用animateKeyframesWithDuration(iOS6以下没有keyFrames 方法)
        [UIView animateWithDuration:0.7 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
            btn.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            // 将btn从内存中移除
            [btn removeFromSuperview];
        }];
    }];
}
@end
